/**
 * 房间中所有用戶数据模型
 * 
 * @author LuoDong
 * @email luo4521@online.net.tw
 * 2010-2-7 9:26
 */
package com.addcn.texas.model
{	
	import com.addcn.common.utils.DestroyMemory;	
	import com.addcn.texas.data.GameUser;
	import com.addcn.texas.data.MyUser;
	import com.addcn.texas.data.RoomUsers;
	import com.addcn.texas.data.User;
	import com.addcn.texas.inter.IUser;
	import flash.display.Bitmap;
	import it.gotoandplay.smartfoxserver.SmartFoxClient;
	import it.gotoandplay.smartfoxserver.SFSEvent;
	
	import com.addcn.fog.model.*;		
	import com.addcn.common.utils.Server;
	import com.addcn.common.data.UserInfoData;
	import com.addcn.common.data.Global;
	import com.addcn.common.event.ParamEvent;
	import com.addcn.texas.utils.DataResolve;
	import com.addcn.texas.utils.GetPhoto;
	
	public class  RoomUserModel extends Model
	{	
		public static const ROOM_USER_DATA_REDY : String = "roomUserDataRedy";
		public static const USER_PHOTO_MODIFY: String = "userPhotoModify";
		public static const USER_SITDOWN :String = "userSitDown";
		public static const MY_SIT_NUM : String  = "mySitNum";
		public static const USER_STANDUP :String = "userStandup";
		public static const SITDOWN_ERR :String = "sitdownErr";				
		public static const UPDATE_USER_CHIP :String = "updateUserChip";		
		public static const EXCHANGE_CHIP:String = "exchangeChip";
		
		private var _server : SmartFoxClient;
		private var _room_user : RoomUsers;  //房间中所有玩家数据对象		
		private var _my_id : int;		//自己的id			
		
		public function RoomUserModel() : void
		{
			_initialize();
		}
		
		private function _initialize() : void
		{				
			this._room_user = RoomUsers.getInstance();
			_server = Server.getInstance();
			_server.addEventListener(SFSEvent.onExtensionResponse, _responseHandler);		
		}
		
		public function setRoomUserData(r_data : Object) : void
		{				
			var _resolve : DataResolve  = DataResolve.getInstance();
			var _user : MyUser = MyUser.getInstance();
			this._my_id = _user.id;			
			var _udata = r_data.u;  //房間中所有玩家数据	
			var _obj : Object = { };			
			var _uid : int;						
			for each(var _val in _udata) {								
				_obj = _resolve._resolveUser(_val);			
				_addUser(_obj);
				if (_obj["sit"] > 0 ) {
					sendNotification(USER_SITDOWN, _obj);  //發送通知,傳遞已經坐下的玩家數據
				}
			}
		}
				
		private function _responseHandler(evt : SFSEvent) : void
		{
			var _data = evt.params.dataObj.dataObj;		
			switch(evt.params.dataObj._cmd) {
				case "userJoin":		
					var _obj = DataResolve.getInstance()._resolveUser(_data.u); //解析數據		
					_addUser(_obj);
					break;
				case "sitDown":														
					_sitDownHandler(_data);					
					break;
				case "standUp":											
					_standUpHandler(_data);				
					break;					
				case "exitRoom":									
																	
					break;
			}
		}				
		
		//=======================================================
		//对外公开方法
		//=======================================================
	
		/**
		 * 获取玩家数据
		 * @param	r_uid int 
		 * @return  Object
		 */
		public function getUserInfo(r_uid : int) : Object
		{
			var _user : GameUser = _room_user.getUser(r_uid);
			var _obj : Object;
			if (_user) {
				_obj = _user.getUserInfo();
			}
			return _obj;
		}
		
		/**
		 * 修改玩家籌碼
		 * @param	r_uid
		 * @param	r_chip
		 */
		public function modifyUserChip(r_uid : int, r_chip : int) : void
		{									
			var _user : GameUser = _room_user.getUser(r_uid);
			var _sit : int = _user.sit;
			_user.chip = r_chip;
			sendNotification(UPDATE_USER_CHIP, { sit:_sit, chip:_user.chip } );	
		}
		
		public function checkUserSit(r_sit : int) : void
		{
			var _user : GameUser = _room_user.getUser(this._my_id);
			if (_user.sit == 0  ) {
				var _model = ModelLocator.getInstance().getModel(RoomModel);
				var _room_data : Object = _model.getRoomData();	
				var _obj : Object= _room_data;
				_obj["gold"] = _user.gold;
				_obj["sit"] = r_sit;
				sendNotification(EXCHANGE_CHIP, _obj);
			}
		}
		
		/**
		 * 坐下操作
		 * @param	r_data Object {sit:int, chip:int}
		 */
		public function sitDown(r_data : Object) : void
		{			
			var _sit : int = r_data["sit"];
			var _chip : int = r_data["chip"];
			var _user : GameUser = _room_user.getUser(_sit, "sit");			
			if (_user) return;
			if (_sit == 0 || _sit > 9) return;					
			var _obj : Object = { }; 
			_obj["ac"] = "sitDown";     
			_obj["sit"] = _sit;  
			_obj["chip"] = _chip;
			_server.sendXtMessage(Server.ext(0), "roomAc", _obj, SmartFoxClient.XTMSG_TYPE_JSON);
		}
		
		/**
		 * 站起操作
		 */
		public function standUp(r_uid : int = 0 ) : void
		{					
			var _user : GameUser = _room_user.getUser(_my_id);
			var _sit : int = _user.sit;			
			if (_sit == 0 || _sit >9) return;
			var _obj : Object = { };
			_obj["ac"] = "standUp";		
			_obj["sit"] = r_uid ==0 ? this._my_id : r_uid;
			_server.sendXtMessage(Server.ext(0), "roomAc", _obj, SmartFoxClient.XTMSG_TYPE_JSON);			
		}
				
		//=======================================================
		// 私有方法 
		//=======================================================		
		
		/**
		 * 將玩家數據加入列表
		 * @param	r_data Object 
		 */
		private function _addUser(r_data : Object) : void
		{	
			var _obj : Object = r_data;					
			var _game_user : GameUser = new GameUser();			
			_game_user.initialize(_obj);			
			_room_user.addUser(_game_user);		
		}
		
		/**
		 * 玩家坐下操作
		 * @param	r_data Object  {sit:int, uid:int, chip:int}
		 */
		private function _sitDownHandler(r_data : Object) : void
		{	
			if (r_data.err != null) {	
				sendNotification(SITDOWN_ERR, {code:r_data.err});
				return;
			}				
			var _sit : int  = r_data["sit"];
			var _uid : int  = r_data["uid"];	
			var _chip : int = r_data["chip"];
			var _user : GameUser = _room_user.getUser(_uid);				
			_user.convertChip(_chip);  //兑换筹码
			_room_user.userSit(_uid, _sit); //记录玩家座位号	
			MonsterDebugger.trace("gameUser",_user.getUserInfo())
			sendNotification(USER_SITDOWN, _user.getUserInfo()); //发送通知，传递玩家数据				
		}
	
		/**
		 * 清除某個座位上對應的玩家
		 * @param	r_data Object
		 */
		private function _standUpHandler(r_data : Object) : void
		{			
			var _uid : int = r_data["uid"];
			var _sit : int = r_data["sit"];						
			var _user : GameUser = _room_user.getUser(_uid);			
			_user.gold = r_data["gold"];
			_user.chip = 0;			
			_room_user.userUp(_uid, _sit);
			sendNotification(USER_STANDUP, r_data );	
		}
						
		/**
		 * 加載玩家頭像圖片		 
		 * @param	r_data Object 玩家數據
		 */
		//private function _loadPhoto(r_data : Object) : void
		//{
			//var _complete : Function
			//var _loader = new GetPhoto();			
			//_loader.setArgument(r_data["photo"],48,48);			
			//_loader.load();				
			//_complete = function(evt :ParamEvent) {			
				//_loader.removeEventListener(GetPhoto.COMPLETE, _complete);
				//r_data["thumb"] = evt.params.pic;								
				//sendNotification(USER_PHOTO_MODIFY, r_data);	
			//}
			//_loader.addEventListener(GetPhoto.COMPLETE, _complete);
		//}
				
		override public function destroy():void 
		{				
			_room_user.destory() ;			
			_room_user = null;
			_server.removeEventListener(SFSEvent.onExtensionResponse, _responseHandler);
			
		}
	}
	
}
